package com.andnux.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.viewbinding.ViewBinding;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class GeneralAdapter<T, V extends ViewBinding> extends ListAdapter<T, GeneralViewHolder> {

    private static final String TAG = "GeneralAdapter";
    private final Context context;
    private OnItemClickListener<T, V> onItemClickListener;
    private OnItemLongClickListener<T, V> onItemLongClickListener;
    private V binding;
    private OnViewBindingListener<V> listener;

    public static interface OnViewBindingListener<V extends ViewBinding> {
        default void onCreateViewBinding(@NonNull V V) {
        }

        ;

        default void onBindViewBinding(@NonNull V V, int position) {
        }

        ;
    }

    public GeneralAdapter(Context context) {
        this(context, null);
    }

    public GeneralAdapter(Context context, OnViewBindingListener<V> listener) {
        super(new DiffUtil.ItemCallback<T>() {
            @Override
            public boolean areItemsTheSame(@NonNull @NotNull T t, @NonNull @NotNull T t1) {
                return t == t1;
            }

            @Override
            public boolean areContentsTheSame(@NonNull @NotNull T t, @NonNull @NotNull T t1) {
                return t.equals(t1);
            }
        });
        this.context = context;
        this.listener = listener;
    }

    @Override
    public void submitList(@Nullable List<T> list) {
        super.submitList(new ArrayList<>(list));
    }

    @NonNull
    @Override
    @SuppressWarnings("all")
    public GeneralViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        try {
            LayoutInflater inflater = LayoutInflater.from(context);
            ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
            Class<T> aClass = (Class<T>) type.getActualTypeArguments()[1];
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            binding = (V) method.invoke(null, inflater, viewGroup, false);
            if (listener != null) {
                listener.onCreateViewBinding(binding);
            }
            return new GeneralViewHolder(binding.getRoot());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setOnItemClickListener(OnItemClickListener<T, V> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener<T, V> onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener<T, V extends ViewBinding> {

        void onItemClick(V view, int position, T t);
    }

    public interface OnItemLongClickListener<T, V extends ViewBinding> {
        boolean onItemLongClick(V view, int position, T t);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull GeneralViewHolder vBaseViewHolder, int position) {
        if (onItemClickListener != null) {
            vBaseViewHolder.itemView.setOnClickListener(view -> {
                onItemClickListener.onItemClick(binding, position, getItem(position));
            });
        }
        if (onItemLongClickListener != null) {
            vBaseViewHolder.itemView.setOnLongClickListener(view -> onItemLongClickListener.onItemLongClick(binding, position, getItem(position)));
        }
        if (listener != null) {
            listener.onBindViewBinding(binding, position);
        }
    }
}
